package com.qs.cwams.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qs.common.framework.config.CommonContextHolder;
import com.qs.cwams.mapper.AtteRuleMapper;
import com.qs.cwams.pojo.domain.AtteRuleDO;

import java.time.LocalDate;

/**
 * @author suncheng
 */
public class AtteRule {
    private final AtteRuleMapper atteRuleMapper;

    private LocalDate date;

    public AtteRule() {
        atteRuleMapper = CommonContextHolder.getBean(AtteRuleMapper.class);
    }

    public AtteRule(LocalDate date) {
        this.date = date;
        atteRuleMapper = CommonContextHolder.getBean(AtteRuleMapper.class);
    }

    public void setDate(int year, int month, int day) {
        this.date = LocalDate.of(year, month, day);
    }

    public AtteRuleDO get() {
        return date != null ? atteRuleMapper.selectOne(new LambdaQueryWrapper<AtteRuleDO>()
                .eq(AtteRuleDO::getAtteYear, date.getYear())
                .eq(AtteRuleDO::getAtteMonth, date.getMonth())
                .eq(AtteRuleDO::getAtteDay, date.getDayOfMonth())) : null;
    }

    public AtteRuleDO get(int year, int month, int day) {
        return atteRuleMapper.selectOne(new LambdaQueryWrapper<AtteRuleDO>()
                .eq(AtteRuleDO::getAtteYear, year)
                .eq(AtteRuleDO::getAtteMonth, month)
                .eq(AtteRuleDO::getAtteDay, day));
    }

    public AtteRuleDO get(LocalDate localDate) {
        return atteRuleMapper.selectOne(new LambdaQueryWrapper<AtteRuleDO>()
                .eq(AtteRuleDO::getAtteYear, localDate.getYear())
                .eq(AtteRuleDO::getAtteMonth, localDate.getMonth().getValue())
                .eq(AtteRuleDO::getAtteDay, localDate.getDayOfMonth()));
    }
}
